//
//  JsImageView.m
//  JsUtilitySdk
//
//  Created by Gikki Ares on 2020/12/20.
//  Copyright © 2020 vgemv. All rights reserved.
//

#import "JsImageView.h"

@implementation JsImageViewVm : JsViewVm


@end

@interface JsImageView()
{
	UIImageView * mIv;
	NSLayoutConstraint * mLc_ivWidth;
	NSLayoutConstraint * mLc_ivHeight;
}


@end

@implementation JsImageView

- (instancetype)initWithFrame:(CGRect)frame {
	if(self = [super initWithFrame:frame]) {
		[self commonInit];
	}
	return self;
}

/**
 从coder中设置vm.
 */
- (instancetype)initWithCoder:(NSCoder *)coder {
	if(self = [super initWithCoder:coder]) {
		JsImageViewVm * vm = [JsImageViewVm new];
		vm.imageSize = self.bounds.size;
		self.vm = vm;
		[self commonInit];
	}
	return self;
}

- (instancetype)initWithVm:(JsViewVm *)vm {
	if(self = [super initWithVm:vm]) {
		[self commonInit];
	}
	return self;
}

//- (instancetype)init {
//	if(self = [super init]) {
//		[self commonInit];
//	}
//	return self;
//}


- (void)commonInit {
	mIv = [[UIImageView alloc]init];
	mIv.translatesAutoresizingMaskIntoConstraints = NO;
	[self addSubview:mIv];
	self.backgroundColor = [UIColor clearColor];
	
	[mIv.centerXAnchor constraintEqualToAnchor:self.centerXAnchor].active = YES;
	[mIv.centerYAnchor constraintEqualToAnchor:self.centerYAnchor].active = YES;
	
	
	(mLc_ivWidth = [mIv.widthAnchor constraintEqualToConstant:self.vm.imageSize.width]).active = YES;;
	(mLc_ivHeight = [mIv.heightAnchor constraintEqualToConstant:self.vm.imageSize.height]).active = YES;

}

- (void)layoutSubviews {
	[super layoutSubviews];
	//代码布局在layoutsubviews还是生效的!
	//mIv.frame = self.bounds;
	if(_isCircle) {
		mIv.layer.cornerRadius = MIN(self.bounds.size.width,self.bounds.size.height)/2;
		mIv.layer.masksToBounds = YES;
	}
}

- (void)setImage:(UIImage *)image {
	self.vm.image = image;
	[self updateVm];
}

- (void)renderVm {
	[super renderVm];
	mIv.image = self.vm.image;
	mLc_ivWidth.constant = self.vm.imageSize.width;
	mLc_ivHeight.constant = self.vm.imageSize.height;
}

@end
